<template>
  <div>
    <el-card>
      <el-button type="primary" @click="showAddExchanage = true">增加库存</el-button>
      <el-row style="margin-top:20px;">
        <el-col :span="4">
          <el-select v-model="status">
            <el-option label="未使用" :value="0"></el-option>
            <el-option label="已使用" :value="1"></el-option>
            <el-option label="全部" :value="''"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-input v-model="exchangeName" placeholder="请输入兑换码"></el-input>
        </el-col>
        <el-col :span="4" :offset="1">
          <el-button type="primary" @click="getData">搜索</el-button>
        </el-col>
      </el-row>

      <el-table :data="exchangeList">
        <el-table-column label="兑换码账号" prop="activateId" align="center"></el-table-column>
        <el-table-column label="兑换码密码" prop="activation" align="center"></el-table-column>
        <el-table-column label="方案包个数" prop="giveCombo" align="center"></el-table-column>
        <el-table-column label="卡类型" align="center">
          <template slot-scope="scope">
            <p v-if="scope.row.type == 1">{{scope.row.type == 1 ?'VIP' :'欧币充值'}}</p>
            <span v-if="scope.row.type == 1">VIP周期：{{scope.row.period}}个月</span>
            <span v-if="scope.row.type == 2">欧币金额：{{scope.row.money}}</span>
          </template>
        </el-table-column>
        <el-table-column label="使用状态" align="center">
          <template slot-scope="scope">
            <p>{{scope.row.status == 0 ?'未使用' :'已使用'}}&nbsp;</p>
            <span v-if="scope.row.status ==1">使用人：{{scope.row.nickName}}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        style="text-align:center;margin-top:20px;"
        layout="prev, pager, next"
        :current-page.sync="currentPage"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog :visible.sync="showAddExchanage" title="添加兑换码">
      <el-form :model="addTicktForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <p>总库存：{{total}}个</p>
        <p>剩余库存：{{enableShare}}个</p>
        <el-form-item label="兑换码类型：">
          <el-button type="primary" size="small" @click="vipShow = !vipShow">VIP兑换码</el-button>
          <el-button type="primary" size="small" @click="OBShow = !OBShow">欧币兑换码</el-button>
        </el-form-item>
        <section v-if="vipShow">
          <el-form-item label="VIP兑换码：">
            <el-row>
              <el-col :span="16">
                <el-table :data="vipList" style="width:370px">
                  <el-table-column label="添加数量" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.num"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="VIP周期(月)" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.period"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
              <el-col :span="3">
                <el-button @click="clickAdd(1)" size="mini" type="success">添加</el-button>
              </el-col>
              <el-col :span="3" :offset="1">
                <el-button @click="clickDelete(1)" size="mini" type="danger">删除</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </section>
        <section v-if="OBShow">
          <el-form-item label="欧币兑换码：">
            <el-row>
              <el-col :span="16">
                <el-table :data="OBList" style="width:370px">
                  <el-table-column label="添加数量" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.num"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                  <el-table-column label="欧币金额" align="cneter">
                    <template slot-scope="scope">
                      <el-input-number
                        controls-position="right"
                        style="width:120px;"
                        v-model="scope.row.money"
                        :min="1"
                      ></el-input-number>
                    </template>
                  </el-table-column>
                </el-table>
              </el-col>
              <el-col :span="3">
                <el-button @click="clickAdd(2)" size="mini" type="success">添加</el-button>
              </el-col>
              <el-col :span="3" :offset="1">
                <el-button @click="clickDelete(2)" size="mini" type="danger">删除</el-button>
              </el-col>
            </el-row>
          </el-form-item>
        </section>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="showAddExchanage = false">取 消</el-button>
        <el-button type="primary" @click="addData()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { activateLists, activate } from "@/api/member";
export default {
  data() {
    return {
      rechargeId: "",
      currentPage: 1,
      exchangeName: "",
      exchangeList: [],
      showAddExchanage: false,
      status: "",
      total: "",
      vipShow: false,
      OBShow: false,
      vipList: [{ num: 0, period: 0, type: 1 }],
      OBList: [{ num: 0, money: 0, type: 2 }],
      exchangeData: [],
      addTicktForm:{},
      enableShare:''
    };
  },
  watch: {
    currentPage(val) {
        
      this.getData(val);
    }
  },
  methods: {
    addData() {
     
      if (this.vipShow && this.OBShow) {
        this.exchangeData = this.vipList.concat(this.OBList);
      } else if (this.vipShow) {
        this.exchangeData = this.vipList;
      } else if (this.OBShow) {
        this.exchangeData = this.OBList;
      } else if (!this.vipShow) {
        this.$message("请选择兑换码类型");
        return;
      }
      let data = {
        exchangeData: this.exchangeData,
        rechargeId:this.rechargeId
      };

      activate(data).then(res => {
        console.log(res);
        this.$message(res.data.data);
      });
      this.showAddExchanage = false;
      this.getData(1);
    },
    getData(page) {
      //   activateLists({ rechargeId: this.rechargeId }).then(res => {
      let data = {
        rechargeId: this.rechargeId,
        status: this.status,
        activation: this.exchangeName,
        page: page
      };
      activateLists(data).then(res => {
        console.log(res);
        this.exchangeList = res.data.data.data.data;
        this.total = res.data.data.data.total;
        this.enableShare = res.data.data.data.total;
      });
      // this.exchangeName = "";
      // this.status = "";
    },
    clickDelete(val) {
      if (val == 1) {
        if (this.vipList.length > 1) {
          this.vipList.pop();
        }
        return;
      } else {
        if (this.OBList.length > 1) {
          this.OBList.pop();
        }
      }
    },
    clickAdd(val) {
      if (val == 1) {
        this.vipList.push({ num: 0, period: 0, type: 1 });
      } else {
        this.OBList.push({ num: 0, money: 0, type: 2 });
      }
    }
  },
  mounted() {
    this.rechargeId = this.$route.query.rechargeId;
    this.getData(1);
  }
};
</script>
